package com.open.capacity.reimburse.controller;


import com.open.capacity.annotation.log.LogAnnotation;
import com.open.capacity.commons.Result;
import com.open.capacity.vo.TreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.list.TreeList;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import com.open.capacity.reimburse.entity.SysFilefolder;
import com.open.capacity.reimburse.service.SysFilefolderService;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文件夹
 *
 * @author
 * @email
 * @date 2021-02-07 11:13:33
 */
@RestController
@RequestMapping("filefolders")
@Api(tags = "文件夹")
public class SysFilefolderController {

    @Autowired
    private SysFilefolderService sysFilefolderService;

    /**
     * 查询tree
     *
     * @param parentId
     * @return
     */
    @PreAuthorize("hasAnyAuthority('sys_filefolder:sysfilefolder:list')")
    @ApiOperation(value = "查询tree")
    @GetMapping("/tree")
    public Result<List<TreeVo>> treeFolders(@RequestParam(value = "parentId", defaultValue = "") String parentId) {
        Map<String, Object> params = new HashMap<>();
        params.put("status",1);
        List<SysFilefolder> folders = sysFilefolderService.findAll(params);
        List<TreeVo> treeList = new TreeList();
        folders.forEach(m -> {
            TreeVo node = new TreeVo();
            node.setId(m.getId());
            node.setText(m.getName());
            node.setTitle(m.getName());
            node.setValue(m.getId());
            node.setShowcheck(false);
            node.setCheckstate(0);
            if (StringUtils.isEmpty(m.getParentId())){
                node.setIsexpand(true);
            }else {
                node.setIsexpand(false);
            }
            node.setComplete(false);
            node.setParentId(m.getParentId());
            treeList.add(node);
        });

        return Result.succeed(TreeVo.RecursiveBuilder(treeList, parentId));
    }

    /**
     * 查询单条记录
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAnyAuthority('sys_filefolder:sysfilefolder:list')")
    public Result findById(String id) {
        return Result.succeed(sysFilefolderService.findById(id));
    }

    /**
     * 保存
     */
    @RequestMapping("/saveOrUpdate")
    @PreAuthorize("hasAnyAuthority('sys_filefolder:sysfilefolder:save')")
    @LogAnnotation(module = "文件夹", name = "保存")
    public Result save(@RequestBody @Valid SysFilefolder sysFilefolder, String keyValue) {
        if (StringUtils.isEmpty(keyValue)) {
            sysFilefolderService.save(sysFilefolder);
        } else {
            sysFilefolder.setId(keyValue);
            sysFilefolderService.update(sysFilefolder);
        }
        return Result.succeed("保存成功");
    }


    /**
     * 删除
     */
    @RequestMapping("/delete/{id}")
    @PreAuthorize("hasAnyAuthority('sys_filefolder:sysfilefolder:delete')")
    @LogAnnotation(module = "文件夹", name = "删除")
    public Result delete(@PathVariable String folderId) {
        sysFilefolderService.delete(folderId);
        return Result.succeed("删除成功");
    }

}
